﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Formality.Parsing.ContextFree.LR
{
#if false
    public class ItemSet : IEnumerable<Item>
    {
        public ItemSet(IEnumerable<Item> Set)
        {
            if (Set.Count() > 5)
            { }

            this.Set = new List<Item>(SortUniques(Set));
        }

        public IEnumerator<Item> GetEnumerator() { return Set.GetEnumerator(); }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); }

        public override bool Equals(object obj)
        {
            var OtherSet = obj as ItemSet;

            if (OtherSet == null)
                return false;

            return Set.SequenceEqual(OtherSet.Set);
        }

        public override int GetHashCode()
        {
            using (new Instrumentation("ItemSet.Hash"))
            {
                int Hash = 0;
                foreach (var Item in Set)
                    Hash ^= Item.GetHashCode();

                return Hash;
            }
        }

        public override string ToString()
        {
            return this.Join(", ");
        }

        private IEnumerable<Item> SortUniques(IEnumerable<Item> Set)
        {
            var Iterator = Set.OrderBy(I => I, new ItemComparer()).GetEnumerator();

            Item Prev = null;
            while (Iterator.MoveNext())
            {
                if (Iterator.Current.Equals(Prev) == false)
                    yield return Iterator.Current;

                Prev = Iterator.Current;
            }
        }

        private IEnumerable<Item> Set;
    }
#else
    public class ItemSet : IEnumerable<Item>
    {
        public ItemSet()
        {
            Unsorted = new List<Item>(100);
        }

        public int Count { get { return Sorted.Count; } }

        public void Add(Item Item)
        {
            if (Unsorted == null)
                throw new Exception("Cannot modify item sets once they have been constructed");

            Unsorted.Add(Item);
        }

        public IEnumerator<Item> GetEnumerator() { return Sorted.GetEnumerator(); }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); }

        public override bool Equals(object obj)
        {
            var OtherSet = obj as ItemSet;

            if (OtherSet == null)
                return false;

            var D1 = Sorted;
            var D2 = OtherSet.Sorted;

            if (D1.Count != D2.Count)
                return false;

            var I1 = D1.GetEnumerator();
            var I2 = D2.GetEnumerator();

            while (I1.MoveNext() && I2.MoveNext())
                if (I1.Current.Equals(I2.Current) == false)
                    return false;

            return true;
        }

        public override int GetHashCode()
        {
            return Hash;
        }

        public override string ToString()
        {
            return this.Join(", ");
        }

        private List<Item> Sorted
        {
            get
            {
                if (sorted == null)
                {
                    if (Unsorted.Count == 0)
                        sorted = Unsorted;
                    else
                    {
                        var Comparer = new ItemComparer();

                        Unsorted.Sort(Comparer);

                        sorted = new List<Item>(Unsorted.Count);

                        Item Prev = null;

                        foreach (var e in Unsorted)
                            if (e.Equals(Prev) == false)
                                sorted.Add(e);

                        Unsorted = null;

                        ComputeHash();
                    }
                }

                return sorted;
            }
        }

        private void ComputeHash()
        {
            Hash = 0;
            foreach (var Item in Sorted)
                Hash ^= Item.GetHashCode();
        }

        private List<Item> Unsorted;
        private List<Item> sorted;
        private int Hash;
    }
#endif
}
